package chapter7;

import java.io.File;

/**
 * 
 * @author Lin...BC
 * 
 */
public class FileLength {
	/**
	 * To get the length of dir
	 * 
	 * @param dir
	 *            The index you want to get length
	 * @return The length of dir
	 */
	public long fileLengthTest(String dir) {

		long len = 0;

		File fl = new File(dir);

		File[] fl2 = fl.listFiles();
		if (fl2 != null) {

			for (int i = 0; i < fl2.length; i++) {

				File ftmp = fl2[i];

				if (ftmp == null) {
				} else {

					if (ftmp.isFile()) {

						len += ftmp.length();

					} else if (ftmp.isDirectory()) {

						String a = ftmp.getAbsolutePath();

						len += fileLengthTest(a);

					}
				}
			}
		}

		return len;

	}

	public static void main(String[] args) {

		FileLength fll = new FileLength();

		File[] roots = File.listRoots();

		long lengthOfDisk;

		for (int i = 0; i < roots.length; i++) {
			
			lengthOfDisk=fll.fileLengthTest(roots[i].getAbsolutePath());
			
			System.out.println("the "+(i+1)+"th disk's length is "+lengthOfDisk);
			
		}
	}

}
